package com.dcm.item.service.impl;

import com.dcm.common.constant.Constants;
import com.dcm.common.exception.MyException;
import com.dcm.item.entity.ItemEntity;
import com.dcm.item.mapper.ItemMapper;
import com.dcm.item.service.ItemService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 商品模块的实现类
 * @author pengrongsheng
 */
@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ItemMapper itemMapper;

    @Override
    public List<ItemEntity> findAll() {
        return itemMapper.findAll();
    }

    @Override
    public ItemEntity findItemById(Long id) throws MyException {
        if (id == null || id <= 0) {
            throw new MyException(Constants.ItemErrorMsg.ITEM_ID_CANNOT_NULL);
        }
        return itemMapper.findOneById(id);
    }

    /**
     * @param item 前台传递的 实体对象
     * @throws MyException
     */
    @Override
    public void addItem(ItemEntity item)  throws MyException {
        //init item
        item.setCreateTime(new Date());
        item.setStatus(0);
        checkItem(item);
        itemMapper.insertItem(item);
    }

    @Override
    public List<ItemEntity> findItemByKeyWords(String keyWords) {
        if (keyWords == null || "".equals(keyWords)) {
            throw new MyException(Constants.ItemErrorMsg.ITEM_NAME_CANNOT_NULL);
        }
        return itemMapper.findItemByKeyWords(keyWords);
    }


    @Override
    public List<ItemEntity> findItemByStatus(Integer status) {
        if (status == null) {
            throw new MyException(Constants.ItemErrorMsg.ITEM_STATUS_CANNOT_NULL);
        }
        return itemMapper.findItemByStatus(status);
    }

    @Override
    public void updateItemStatus(ItemEntity item) throws MyException {
        checkItem(item);
        itemMapper.updateItem(item);
    }

    @Override
    public List<ItemEntity> searchItem(Integer specId, Long id, String itemName) {
        return itemMapper.searchItem(specId,id,itemName);
    }

    @Override
    public PageInfo<ItemEntity> findPage(Integer currentPage, Integer pageSize ) {
        if (pageSize == null || currentPage == null) {
            throw new MyException(Constants.ResponseMsg.PAGE_PARAMS_NOT_NULL);
        }
        PageHelper.startPage(currentPage, pageSize);
        List<ItemEntity> list = itemMapper.findAll();
        //用PageInfo对结果进行包装
        PageInfo<ItemEntity> page = new PageInfo<>(list);

        return page;
    }

    /**
     * 检查实体类参数
     * @param item
     */
    private static void checkItem(ItemEntity item)  {
        if (item.getStatus() == null) {
            throw new MyException(Constants.ItemErrorMsg.ITEM_STATUS_CANNOT_NULL);
        }
        if (StringUtils.isBlank(item.getItemName())) {
            throw new MyException(Constants.ItemErrorMsg.ITEM_NAME_CANNOT_NULL);
        }
        if (item.getCreateTime() == null) {
            throw new MyException(Constants.ItemErrorMsg.ITEM_CREATE_TIME_CANNOT_NULL);
        }
        if (item.getBank() == null) {
            throw new MyException(Constants.ItemErrorMsg.ITEM_BANK_CANNOT_NULL);
        }
        if (item.getPrice() == null) {
            throw new MyException(Constants.ItemErrorMsg.ITEM_PRICE_CANNOT_NULL);
        }
        if (StringUtils.isBlank(item.getIntroduction())) {
            throw new MyException(Constants.ItemErrorMsg.ITEM_INTRODUCTION_CANNOT_NULL);
        }
    }
}
